package com.ffcc.fitness.user.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.ffcc.fitness.base.response.JsonResult;
import com.ffcc.fitness.user.pojo.dto.UserLoginParam;
import com.ffcc.fitness.user.pojo.dto.UserSaveParam;
import com.ffcc.fitness.user.pojo.dto.UserUpdateParam;
import com.ffcc.fitness.user.pojo.entity.TokenInfo;
import com.ffcc.fitness.user.pojo.vo.UserVO;
import com.ffcc.fitness.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@SaIgnore
@RequestMapping("v1/user/")
public class UserController {
    @Autowired
    private UserService userService;

    //注册
    @PostMapping("reg")
    public JsonResult reg(UserSaveParam userSaveParam) {
        userService.reg(userSaveParam);
        return JsonResult.ok();
    }    //登录
    @PostMapping("login")
    public JsonResult login(UserLoginParam userLoginParam) {
        UserVO userVO = userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }

    // 获取用户完整信息（包括特殊身份，如健身教练的额外信息）
    @GetMapping("select")
    public JsonResult select(@RequestParam String username) {
        UserVO userVO = userService.selectByUsername(username);
        return JsonResult.ok(userVO);
    }
    
    // 根据userId获取用户信息
    @GetMapping("selectByUserId")
    public JsonResult selectByUserId(@RequestParam Long userId) {
        UserVO userVO = userService.selectByUserId(userId);
        return JsonResult.ok(userVO);
    }

    //修改用户信息
    @PostMapping("update")
    public JsonResult update(UserUpdateParam userUpdateParam) {
        UserVO userVO = userService.update(userUpdateParam);
        log.info("controller里返回的updateVO：" + userVO);
        return JsonResult.ok(userVO);
    }

    //是否登录(返回true/false)
    @PostMapping("isLogin")
    public JsonResult isLogin() {
        return JsonResult.ok("是否登录：" + StpUtil.isLogin());
    }
    //查询token信息
    @PostMapping("tokenInfo")
    public JsonResult tokenInfo() {
        try {
            if (!StpUtil.isLogin()) {
                // 未登录，返回空的token信息
                TokenInfo tokenInfo = new TokenInfo();
                tokenInfo.setIsLogin(false);
                return JsonResult.ok(tokenInfo);
            }
            
            // 获取Sa-Token的原生token信息
            cn.dev33.satoken.stp.SaTokenInfo saTokenInfo = StpUtil.getTokenInfo();
              // 转换为自定义的TokenInfo结构
            TokenInfo tokenInfo = new TokenInfo();
            tokenInfo.setTokenName(saTokenInfo.getTokenName());
            tokenInfo.setTokenValue(saTokenInfo.getTokenValue());
            tokenInfo.setIsLogin(true); // 明确设置登录状态
            tokenInfo.setLoginId(String.valueOf(saTokenInfo.getLoginId()));
            tokenInfo.setLoginType(saTokenInfo.getLoginType());
            tokenInfo.setTokenTimeout(saTokenInfo.getTokenTimeout());
            tokenInfo.setSessionTimeout(saTokenInfo.getSessionTimeout());
            tokenInfo.setTokenSessionTimeout(saTokenInfo.getTokenSessionTimeout());
            tokenInfo.setTokenActiveTimeout(saTokenInfo.getTokenActiveTimeout());
            // loginDevice字段在Sa-Token中可能不存在，设置为默认值
            tokenInfo.setLoginDevice("web");
            
            return JsonResult.ok(tokenInfo);
        } catch (Exception e) {
            log.error("获取token信息失败", e);
            TokenInfo tokenInfo = new TokenInfo();
            tokenInfo.setIsLogin(false);
            return JsonResult.ok(tokenInfo);
        }
    }

    //退出登录
    @PostMapping("logout")
    public JsonResult logout() {
        StpUtil.logout();
        return JsonResult.ok();
    }


}
